class FightersController < ApplicationController
  before_filter :authenticate_user!, except: %w(index show)
  before_action :set_fighter, only: [:show, :edit, :update, :destroy]

  def user
    @fighters = Fighter.where(:user_id => current_user.id)
  end

  def index
    @fighters = Fighter.all
  end

  def show
    pie_chart
  end

  def new
    @fighter = Fighter.new
  end

  def edit
  end

  def create
    @fighter = Fighter.new(fighter_params)
    @fighter.user = current_user

    respond_to do |format|
      if @fighter.save
        format.html { redirect_to @fighter, notice: 'Fighter was successfully created.' }
        format.json { render action: 'show', status: :created, location: @fighter }
      else
        format.html { render action: 'new' }
        format.json { render json: @fighter.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|

      if params[:fighter]["crop_x"].present?
        @fighter.crop_img = params[:fighter]["crop_img"]
        @fighter.crop_x = params[:fighter]["crop_x"]
        @fighter.crop_y = params[:fighter]["crop_y"]
        @fighter.crop_h = params[:fighter]["crop_h"]
        @fighter.crop_w = params[:fighter]["crop_w"]
      end

      if @fighter.update(fighter_params)
          format.html { redirect_to @fighter, notice: 'Fighter was successfully updated.' }
          format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @fighter.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @fighter.destroy
    respond_to do |format|
      format.html { redirect_to fighters_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_fighter
      @fighter = Fighter.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def fighter_params
      params[:fighter].permit(:name, :nickname, :born, :avatar, :poster, :weight, :height, :city, :country, :zip,
                              :gender, :state, :phone, :skill_style_1, :skill_trained_1, :skill_belt_1, :skill_style_2, 
                              :skill_trained_2, :skill_belt_2, :disclaim_accept,
                              :crop_img, :crop_x, :crop_y, :crop_w, :crop_h, :height_feet, :height_inches)
    end

    def pie_chart
      data_table = GoogleVisualr::DataTable.new
      data_table.new_column('string', 'Task')
      data_table.new_column('number', 'Hours per Day')
      data_table.add_rows(3)
      data_table.set_cell(0, 0, 'STRIKING'     )
      data_table.set_cell(0, 1, 11 )
      data_table.set_cell(1, 0, 'TAKEDOWNS'      )
      data_table.set_cell(1, 1, 2  )
      data_table.set_cell(2, 0, 'SUBMISSIONS'  )
      data_table.set_cell(2, 1, 2  )

      opts   = { 
        :width => 400, 
        :height => 240, 
        :title => 'Performance',
        :pieHole => 0.4,
        :backgroundColor => 'transparent',
        #:colors => ['#cd0000', '#8b8682', '#802a2a'],
     
      }
      @chart = GoogleVisualr::Interactive::PieChart.new(data_table, opts)
    end
    
end
